<?php
namespace api\modules\v1\controllers;

use api\modules\v1\models\user\UserSms;
use common\models\im\Im;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

/**
 * Default controller for the `v1` module
 */
class SmsController extends DefaultController
{

    public $modelClass = '';
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                    'login-code', 'register-code', 'forget-code','place-code'
                ]
            ]
        ]);
    }

    /**
     * 发送注册验证码
     * @return array
     */
    public function actionRegisterCode() {

        if (Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('registerCode');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->sendPhoneCode();
            }
        }
    }

    /**
     * 发送登录验证码
     * @return array
     */
    public function actionLoginCode() {
        if (\Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('loginCode');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->sendPhoneCode();
            }
        }
    }

    /**
     * 忘记密码验证码
     * @return array
     */
    public function actionForgetCode() {
        if (Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('forgetPasswordCode');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->sendPhoneCode();
            }
        }
    }

    /**
     * 修改密码
     * @return array
     */
    public function actionModifyPasswordCode()
    {
        if (Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('modifyPasswordCode');
            $data['login_name'] = "";
//            if ($model->load(Yii::$app->request->post(), '')) {
            if ($model->load($data, '')) {
                return $model->sendPhoneCode();
            }
        }
    }

    /**
     * 支付密码验证码（添加和修改）
     * @return array
     */
    public function actionPayCode() {
        if (Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('payPasswordCode');
            $data['login_name'] = "";
//            if ($model->load(Yii::$app->request->post(), '')) {
            if ($model->load($data, '')) {
                return $model->sendPhoneCode();
            }
        }
    }

    /**
     * 校验异地登录
     * @return array
     */
    public function actionPlaceCode() {
        if (Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('verifyPlaceCode');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->sendPhoneCode();
            }
        }
    }

    /**
     * 钱包开关验证码
     * @return array
     */
    public function actionMoneySwitchCode() {
        if (Yii::$app->request->isPut) {
            $model = new UserSms();
            $model->setScenario('moneySwitchCode');
            $data['login_name'] = "";
            if ($model->load($data, '')) {
                return $model->sendPhoneCode();
            }
        }
    }
}
